package com.topcoder.srm521;

public class RedAndGreen {
	public int minPaints2(String row) {
		int min = Integer.MAX_VALUE;
		for (int red = 0; red <= row.length(); red++) {
			int count = 0;

			for (int i = 0; i < red; i++) {
				if (row.charAt(i) == 'G')
					count++;
			}

			for (int i = red; i < row.length(); i++) {
				if (row.charAt(i) == 'R')
					count++;
			}

			min = Math.min(min, count);
		}

		return min;
	}

	public int minPaints(String row) {
		int n = row.length();
		int totR = 0;
		for (int p = 0; p < n; p++)
			if (row.charAt(p) == 'R')
				totR++;
		int res = n;
		int r = 0;
		for (int p = 0; p <= n; p++) {
			int tmp = (p - r) + (totR - r);
			if (tmp < res)
				res = tmp;
			if (p < n && row.charAt(p) == 'R')
				r++;
		}
		return res;
	}
}
